var resultData="";
var allowEmail,allowPassword,allowPassword2,allowName,allowVerifyCode;
function InitRequest(){
	jQ("div.reg_choose input").eq(0).attr("checked","checked");
	jQ("#type_1 div.register_box div.reg_type input").eq(0).attr("checked","checked");
	jQ("#type_1 div.register_box input").each(function(i,data){
		if(jQ(data).attr("type")!="button"){
			jQ(data).val('');
		}
	});
}
function changeImg(obj) {
	var imgSrc = jQ(obj).parent().find("img");
	var codeurl = ctx + "/verifycode.do";
	var urlurl = codeurl + "?time=" + (new Date()).valueOf();
	imgSrc.attr("src", urlurl);
}
function binding(obj) {
	jQ('#type_1').hide();
	jQ('#type_2').hide();
	jQ('#type_' + obj).show();
}
function loadRquestInfo(r) {
	jQ.ajax( {
		type : "POST",
		url : "contactAjax!doRegisterContact.action",
		data : {
			content : r,
			jaction : "loadRquestInfo"
		},
		success : function(data) {
			if(data==""){
				jalert("无效的链接",function(){window.location.href=ctx+"/index.html";});
			}else{
				resultData=data;
				if (jQ(data).find("root").text() != "") {
					var selfEmail = jQ(data).find("selfUser").find("email").text();
					jQ("#type_1 div.register_box div.reg_type div").eq(0).find("span").empty().append("使用" + selfEmail + "注册");
					jQ("#type_1 div.register_box div.email input").val(selfEmail);
	
					jQ("#type_2 div.register_box div.agree").eq(0).find("span").eq(1).empty().append("绑定" + selfEmail);
				}
			}
		}
	});
}
jQ(function() {
	if (r == "")
		return;
	InitRequest();
	loadRquestInfo(r);
});
/*** 验证***/
//Email;
function isEmail(s){
 	var p = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/i; 
 	return p.test(s);
}
function checkExistEmail(obj){
	var b = jQ(obj).parent().find("b");
	if(isEmail(trim(jQ(obj).val()))){
		jQ.ajax({
			type: "POST",
			url: "userAjax!checkUser.action",
			data:{
				emailInput : trim(jQ(obj).val())
			},
			success:function(data){
				if(data=="0"){
					jQ(b).removeClass().addClass("wrong").empty().append("邮箱地址已被使用,请重新输入.");
					allowEmail = false;
				}else{
					jQ(b).removeClass().addClass("right").empty();
					allowEmail = true;
				}
			}
		});
	}else{
		jQ(b).removeClass().addClass("wrong").empty().append("请输入正确的邮箱地址.");
		allowEmail = false;
		return false;
	}
}
function checkPassWord(obj){
	var b = jQ(obj).parent().find("b");
	var p = /^[a-zA-Z0-9][a-zA-Z0-9_-]{5,19}$/;
	if(p.test(trim(jQ(obj).val()))){
		jQ(b).removeClass().addClass("right").empty();
		allowPassword = true;
	}else{
		jQ(b).removeClass().addClass("wrong").empty().append("密码只能为a-z0-9之间.");
		allowPassword = false;
	}
}
function checkpassWord2(obj){
	var b = jQ(obj).parent().find("b");
	var pass1=trim(jQ(obj).parent().prev().find("input").val());
	var pass2=trim(jQ(obj).val());
	if(pass2.length<=0){
		allowPassword2 = false;
	}else if(pass2==pass1){
		jQ(b).removeClass().addClass("right").empty();
		allowPassword2 = true;
	}else{
		jQ(b).removeClass().addClass("wrong").empty().append("密码与再次重复密码不一致.");
		allowPassword2 = false;
	}
}
function checkName(obj){
	var b = jQ(obj).parent().find("b");
	var p = /[A-Za-z0-9\u4e00-\u9fa5]{2,20}$/;
	var name = trim(jQ(obj).val());
	if(p.test(name)){
		jQ(b).removeClass().addClass("right").empty();
		allowName = true;
	}else{
		jQ(b).removeClass().addClass("wrong").empty().append("姓名不在5-20个字符以内或包含非法字符,请重新输入.");
		allowName = false;
	}
}
function checkVerifyCode(obj){
	var b = jQ(obj).parent().find("b");
	var code = trim(jQ(obj).val());
	if(code.length<=0){
		jQ(b).removeClass().addClass("wrong").empty().append("请输入验证码.");
		allowVerifyCode = false;
	}else{
		jQ.ajax({
			type: "POST",
			url: "userAjax!checkYzm.action",
			data:{
				vcodeInput : code
			},
			success:function(data){
				if(data=="1"){
					jQ(b).removeClass().addClass("right").empty();
					allowVerifyCode = true;
				}else{
					jQ(b).removeClass().addClass("wrong").empty().append("验证码错误,请重新输入.");
					allowVerifyCode = false;
				}
			}
		});
	}
}
function checkEmail(obj){
	var b = jQ(obj).parent().find("b");
	var email=trim(jQ(obj).val());
	if(isEmail(email)){
		jQ(b).removeClass().addClass("right").empty();
		return true;
	}else{
		jQ(b).removeClass().addClass("wrong").empty().append("请输入正确的邮箱地址.");
		return false;
	}
}
/*** end***/
function itEmail(){
	if(resultData!=""){
		jQ('#type_1 div.register_box div.email input').attr('disabled','disabled').val(jQ(resultData).find("selfUser").find("email").text());
	}
}
function otherEmail(){
	if(resultData!=""){
		jQ('#type_1 div.register_box div.email input').removeAttr('disabled').val('');
	}
}
function registerRequestSubmit(){
	var mode=jQ("div.reg_choose input");
	if(jQ(mode).eq(0).attr("checked")){//注册
		var email=jQ(resultData).find("selfUser").find("email").text();
		if(!jQ('#type_1 div.register_box div.email input').attr('disabled')){
			email=jQ('#type_1 div.register_box div.email input').val();
		}else{
			allowEmail=true;
		}
		if(allowEmail&&allowPassword&&allowPassword2&&allowName&&allowVerifyCode){
			var password=jQ('#type_1 div.register_box div.password input').val();
			var password2=jQ('#type_1 div.register_box div.password_repeat input').val();
			var name=jQ('#type_1 div.register_box div.real_name input').val();
			var code=jQ('#type_1 div.register_box div.wrap input').val();
			var flag=jQ('#type_1 div.register_box div.agree input');
			if(flag.eq(0).attr("checked")){
				flag=1;
			}else if(flag.eq(1).attr("checked")){
				flag=0;
			}
			jQ.ajax({
				type: "POST",
				url: "contactAjax!doRegisterContact.action",
				data:{
					emailInput : trim(email),
					pwdInput : trim(password),
					pwdInput2 : trim(password2),
					nameInput : trim(name),
					vcodeInput : trim(code),
					jaction : "register",
					content : r,
					id : flag
				},
				success:function(data){
					jalert(jQ(data).find("message").text(),function(){window.location.href=ctx+"/register_activate.html?activate=1";});
				}
			});
		}else{
			jalert("请填写完整信息");
		}
	}else if(jQ(mode).eq(1).attr("checked")){//登陆
		var email=trim(jQ("#type_2 div.register_box div.email input").val());
		var password=trim(jQ("#type_2 div.register_box div.password input").val());
		var bind;
		var addftg;
		
		var agree=jQ("#type_2 div.register_box div.agree");
		if(jQ(agree).eq(0).find("input").eq(0).attr("checked")){
			//绑定
			bind=1;
		}else if(jQ(agree).eq(0).find("input").eq(1).attr("checked")){
			//不绑定
			bind=2;
		}
		if(jQ(agree).eq(1).find("input").eq(0).attr("checked")){
			//加好友
			addftg=1;
		}else if(jQ(agree).eq(1).find("input").eq(1).attr("checked")){
			//不加好友
			addftg=2;
		}
		var p = /^[a-zA-Z0-9][a-zA-Z0-9_-]{5,19}$/;
		if(isEmail(email)&&p.test(password)){
			jQ.ajax({
				type: "POST",
				url: "contactAjax!doRegisterContact.action",
				data:{
					jaction : "login",
					emailInput : email,
					pwdInput : password,
					bind : bind,
					addftg : addftg,
					content : r
				},
				success:function(data){
					jalert(jQ(data).find("message").text(),function(){window.location.href=ctx+"/index.html";});
				}
			});
		}else{
			jalert("请输入正确的账号或密码");
		}
	}
}
function jalert(info,LFun){
	jQ("#ALERT p").html(info);
	if(LFun!=null)jQ("#ALERT").bind("LFun",LFun);
	jQ("#ALERT").dialog('open');
}